package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.contast.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    @RequestMapping("/getMenuList")
    public Result getMenuList(String username){
        try {
            List<Menu> menuList= menuService.findMenuList(username);
            return  new Result(true, MessageConstant.GET_MENU_FAIL,menuList);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_MENU_FAIL);
        }


    }

    // 查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.pageQuery(queryPageBean);

    }
    // 添加
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
      /*  if(menu.getParentMenuId().equals("会员管理")){
            menu.setParentMenuId(1);
        }else if(menu.getParentMenuId().equals("预约管理")){
            menu.setParentMenuId(5);
        }else if(menu.getParentMenuId().equals("健康评估")){
            menu.setParentMenuId(11);
        }else if(menu.getParentMenuId().equals("统计分析")){
            menu.setParentMenuId(13);
        }else if(menu.getParentMenuId().equals("系统设置")){
            menu.setParentMenuId(15);
        }*/
        try {
            menuService .add(menu);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
    }

    // 编辑前的查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    // 编辑
    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    // 删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            List<Menu> menuList =   menuService.findChildren(id);
            if(menuList!=null){
                for (Menu menu : menuList) {
                    menuService.delete(menu.getId());
                }
            }
            menuService.delete(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }



}
